<?php
// +----------------------------------------------------------------------
// | Azthink通用开发平台 [ TOMORROW IS ANOTHER DAY ]
// +----------------------------------------------------------------------
// | Copyright (c) 2018-2019 http://www.azthink.top All rights reserved.
// +----------------------------------------------------------------------
// | Author 李忠<ny_lizhong@126.com>
// +----------------------------------------------------------------------
namespace app\admin\controller;

use app\admin\model\Member as MemberModel;
use app\admin\model\Office as OfficeModel;
use think\Validate;

/**
 * 用户控制器
 *
 * @author 李忠<ny_lizhong@126.com>
 * @createAt 2018-06-20 17:55:59
 *
 */
class Member extends Admin {

    /**
     * 列表页面
     */
    public function index($title = null, $status = null, $office = null) {
        cookie('__forward__', $this->request->url());

        // 数据列表
        $data_list = MemberModel::lists($title, $status, $office);
        $this->assign('data_list', $data_list);
        $this->assign('pager', $data_list->render());

        $this->assign('office_tree', list_to_config(OfficeModel::toFormatTree('admin'), 'id', 'title_show'));
        $this->assign('status', $status);
        $this->assign('office', $office);
        $this->assign('title', $title);
        $this->setMeta('用户管理');
        return $this->fetch();
    }

    // 更新个人信息
    public function modifyPwd($id = null) {
        if ($this->request->isPost()) {
            // 表单数据
            $data = $this->request->post();
            // 验证
            $rule = ['password' => 'require|length:4,12'];
            $msg = [
                'password.require' => '新密码不能为空',
                'password.between' => '新密码长度必须在4到12位之间'
            ];
            $validate = Validate::make($rule, $msg);

            $result = $validate->check($data);

            if (true !== $result) {
                $this->error($validate->getError());
            }
            if ($data = MemberModel::modifyPwd($data['password'], $data['id'])) {

                $this->log($id, '更新密码成功');
                $this->success('更新密码成功', cookie('__forward__'));
            } else {
                $this->error('更新密码失败');
            }

        } else {
            $info = MemberModel::get($id);
            $this->assign('info', $info);
            $this->setMeta('修改密码');
            return $this->fetch('modifyPwd');
        }
    }

    public function selfPwd(){
        if ($this->request->isPost()) {
            // 表单数据
            $data = $this->request->post();
            // 验证
            $rule = ['password' => 'require|length:4,12'];
            $msg = [
                'password.require' => '新密码不能为空',
                'password.between' => '新密码长度必须在4到12位之间'
            ];
            $validate = Validate::make($rule, $msg);

            $result = $validate->check($data);

            if (true !== $result) {
                $this->error($validate->getError());
            }

            if ($data = MemberModel::modifyPwd($data['password'], $data['id'])) {

                $this->log(UID, '更新密码成功');
                $this->success('更新密码成功');
            } else {
                $this->error('更新密码失败');
            }

        } else {
            $info = MemberModel::get(UID);
            $this->assign('info', $info);
            $this->setMeta('修改密码');
            return $this->fetch('selfpwd');
        }
    }

    public function profile(){
        if ($this->request->isPost()) {
            // 表单数据
            $data = $this->request->post();
            // 验证
            $rule = ['title' => 'require'];
            $msg = [
                'password.require' => '用户姓名不能为空',
            ];
            $validate = Validate::make($rule, $msg);

            $result = $validate->check($data);

            if (true !== $result) {
                $this->error($validate->getError());
            }

            if (MemberModel::where('id', UID)->update($data)) {
                /* 登录记录 session和cookies*/
                $auth = session('user_auth');
                $auth['title'] = $data['title'];

                session('user_auth', $auth);
                session('data_auth_sign', data_auth_sign($auth));

                $this->log(UID, '更新个人信息成功');
                $this->success('更新个人信息成功');
            } else {
                $this->error('更新个人信息失败');
            }

        } else {
            $info = MemberModel::get(UID);
            $this->assign('info', $info);
            $this->setMeta('更新个人信息');
            return $this->fetch('profile');
        }
    }


    /**
     * 禁用
     * @param unknown $id
     */
    public function forbid($id) {
        if (MemberModel::state($id, 0)) {
            $this->log($id, '禁用用户成功');
            $this->success('禁用用户成功');
        } else {
            $this->error('禁用用户失败');
        }
    }

    /**
     * 恢复
     * @param unknown $id
     */
    public function resume($id) {
        if (MemberModel::state($id, 1)) {
            $this->log($id, '启用用户成功');
            $this->success('启用用户成功');
        } else {
            $this->error('启用用户失败');
        }
    }

    /**
     * 删除
     * @param $id
     */
    public function delete($id) {
        if (MemberModel::state($id, -1)) {
            $this->log($id, '删除用户成功');
            $this->success('删除用户成功');
        } else {
            $this->error('删除用户失败');
        }
    }
}